﻿@model Video
@{
    ViewBag.Title = "Video Details - " + Model.Title;
    var feedback = Model.Feedbacks.ToList();
    //get a list of first and last names of the people who submitted feedback
    var ids = feedback.Select(f => f.UserInteropId).ToArray();
    var interops = from i in ContextManager.CurrentDb.UserInterops
                   where ids.Contains(i.Id)
                   select i;
    var userMap = interops.ToList().Select(i => new UserProfile(i)).ToDictionary(u => u.Interop.Id);

    //these stats are really just a proof of concept, there's a lot more work to be done here...
    var stats = ContextManager.CurrentDb.VideoEvents.Where(e => e.Video.Id == Model.Id).ToList();
    int views = 0;
    int completeViews = 0;
    foreach (var vidEvent in stats)
    {
        if (vidEvent.EventName == "Started") { views++; }
        else if (vidEvent.EventName == "Ended") { completeViews++; }
    }
}
<p>Below is the feedback for <strong>@Model.Title</strong>, and where video usage statistics will be displayed.</p>
<fieldset>
    <legend>Feedback</legend>
    @if (feedback.Count > 0)
    {
        <table style="margin-top: 10px; margin-bottom: 0px; width: 100%;">
            <thead>
                <tr>
                    <th>User</th>
                    <th>Date Posted</th>
                    <th>Comments</th>
                    <th>Contact</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var f in feedback)
                {
                    var u = userMap[f.UserInteropId];
                    <tr>
                        <td>@u.NameFormatted()</td>
                        <td>@f.DatePosted.ToShortDateString()</td>
                        <td>@f.Comments</td>
                        <td><a href="mailto:@u.EmailAddress">@u.EmailAddress</a></td>
                    </tr>   
                }
            </tbody>
        </table>   
    }
    else 
    {
        <p class="padtop last">There is no feedback for this video.</p>
    }  
</fieldset>
<fieldset>
    <legend>Statistics</legend>
    @if (stats.Count > 0)
    {
        <p class="padtop">Total Views: @views<br />Complete Views: @completeViews</p>
        <table class="med">
            <thead>
                <tr>
                    <th>User</th>
                    <th>Event</th>
                    <th>Date</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var e in stats.OrderByDescending(e => e.DatePosted))
                {
                    <tr>
                        <td>@e.User.UserName</td>
                        <td>@e.EventName</td>
                        <td>@e.DatePosted</td>
                    </tr>
                }
            </tbody>
        </table>
    }
    else
    {
        <p class="padtop">This video has not been watched by any users.</p>
    }
</fieldset>